package com.kzn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kzn.entity.GlobalVar;
import com.kzn.entity.RewardPoints;
import com.kzn.entity.RoughTask;
import com.kzn.entity.TimeTag;
import com.kzn.mapper.CrowdMapper;
import com.kzn.mapper.RewardPointsMapper;
import com.kzn.mapper.RoughTaskMapper;
import com.kzn.service.IRoughTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sorclown
 * @since 2022-02-05
 */
@Service
public class RoughTaskServiceImpl extends ServiceImpl<RoughTaskMapper, RoughTask> implements IRoughTaskService {

    private RoughTaskMapper roughTaskMapper;
    private RewardPointsMapper rewardPointsMapper;
    private CrowdMapper crowdMapper;

    public RoughTaskServiceImpl() {
    }

    @Autowired
    public RoughTaskServiceImpl(RoughTaskMapper roughTaskMapper, RewardPointsMapper rewardPointsMapper,CrowdMapper crowdMapper) {
        this.roughTaskMapper = roughTaskMapper;
        this.rewardPointsMapper = rewardPointsMapper;
        this.crowdMapper = crowdMapper;
    }

    @Override
    public boolean uploadResultByTask(List<TimeTag> timeTags, Integer taskId) {
        QueryWrapper<RoughTask> roughTaskQueryWrapper = new QueryWrapper<>();
        roughTaskQueryWrapper.eq("id", taskId);
        RoughTask roughTask = roughTaskMapper.selectOne(roughTaskQueryWrapper);
        String resultPath = GlobalVar.UPLOAD_PATH + "/" + roughTask.getProjectName() + "/result/" + roughTask.getSourceFilePath().split("\\.")[0];


        //本地保存的格式
        String suffix = ".txt";
        //result.path 应该为 upload_path + "/" + projectName + "/result/" + taskResourceFileName(无后缀) + 后缀
        File result = new File(resultPath + suffix);

        //resultFolder.path 应该为 upload_path + "/" + projectName + "/result/"
        File resultFolder = new File(result.getParent());
        if (!resultFolder.exists()) {
            resultFolder.mkdirs();
        }

        try (BufferedWriter bw = new BufferedWriter(new FileWriter(result.getPath()));) {
            for (int i = 0; i < timeTags.size(); i++) {
                bw.append(timeTags.get(i).toString() + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        return true;
    }

    @Override
    public boolean submit(Integer taskId, Integer crowdId) {
        QueryWrapper<RoughTask> roughTaskQueryWrapper = new QueryWrapper<>();
        roughTaskQueryWrapper.eq("id", taskId);
        roughTaskQueryWrapper.eq("status", 0);
        RoughTask roughTask = roughTaskMapper.selectOne(roughTaskQueryWrapper);

        if (null == roughTask) {
            return false;
        }
        Integer awardPoint = roughTask.getAwardPoint();
        if (null == crowdMapper.selectById(crowdId) ){
            return false;
        }

        QueryWrapper<RewardPoints> rewardPointsQueryWrapper = new QueryWrapper<>();
        rewardPointsQueryWrapper.eq("crowdId", crowdId);
        if (rewardPointsMapper.exists(rewardPointsQueryWrapper)) {
            //如果是表中已经有了该标注人员
            //从表中获取 rewardPoints 对象
            RewardPoints rewardPoints = rewardPointsMapper.selectOne(rewardPointsQueryWrapper);
            //增加对应点数
            Integer curPoint = rewardPoints.getPoint() + awardPoint;
            rewardPoints.setPoint(curPoint);
            rewardPointsMapper.updateById(rewardPoints);
        } else {
            //如果该标注人员是第一次提交子任务
            //创建 rewardPoints 对象
            RewardPoints rewardPoints = new RewardPoints();
            rewardPoints.setCrowdId(crowdId);
            //保存对应点数
            rewardPoints.setPoint(awardPoint);
            rewardPointsMapper.insert(rewardPoints);
        }

        //将子任务状态改为已完成
        roughTask.setStatus(1);
        roughTaskMapper.updateById(roughTask);
        return true;
    }

}
